# 引入qt
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH};${QT6_HOME})

message("prefix : ${CMAKE_PREFIX_PATH}")

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

find_package(Qt6 COMPONENTS
        Core
        Gui
        Widgets
        PrintSupport
        REQUIRED)


qt_add_executable(MediaTranscoder MANUAL_FINALIZATION main.cpp
        widgets/dropupload.cpp
        widgets/dropupload.h
        widgets/dropupload.ui
        widgets/fileselector.cpp
        widgets/fileselector.h
        widgets/fileselector.ui
        widgets/uploadbar.cpp
        widgets/uploadbar.h
        widgets/uploadbar.ui
        res.qrc
        resource.rc
        mainwindow.cpp
        mainwindow.h
        mainwindow.ui
        widgets/videopreview.cpp
        widgets/videopreview.h
        widgets/videopreview.ui
        widgets/ImageWidget.cpp
        widgets/ImageWidget.h
        widgets/audioencodeconfigitem.cpp
        widgets/audioencodeconfigitem.h
        widgets/audioencodeconfigitem.ui
        widgets/videoencodeconfigwidget.cpp
        widgets/videoencodeconfigwidget.h
        widgets/videoencodeconfigwidget.ui
        widgets/IconButton.cpp
        widgets/IconButton.h
        widgets/transcoderqueuewidget.cpp
        widgets/transcoderqueuewidget.h
        widgets/transcoderqueuewidget.ui
        widgets/QueueItem.cpp
        widgets/QueueItem.h
)
target_link_libraries(MediaTranscoder PRIVATE
        Qt::Core
        Qt::Gui
        Qt::Widgets
        Qt::PrintSupport
        glog::glog
        utils
        muxer
        demuxer
)

qt_add_resources(${CMAKE_CURRENT_SOURCE_DIR}/res.qrc)

if (WIN32)
    add_custom_command(TARGET MediaTranscoder POST_BUILD
            COMMAND ${QT6_HOME}/../../bin/windeployqt6 "$<TARGET_FILE:MediaTranscoder>")
endif ()

add_custom_command(TARGET MediaTranscoder POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy
        ${PROJECT_SOURCE_DIR}/src/gui/config/preset.json
        ${PROJECT_SOURCE_DIR}/src/gui/config/setting.json
        "$<TARGET_FILE_DIR:MediaTranscoder>")

if (WIN32 AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
    set(DEBUG_SUFFIX)
    if (MSVC AND CMAKE_BUILD_TYPE MATCHES "Debug")
        set(DEBUG_SUFFIX "d")
    endif ()
    set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")
    if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
        set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
            set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        endif ()
    endif ()
    if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E make_directory
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
    endif ()
    foreach (QT_LIB Core Gui Widgets)
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/bin/Qt6${QT_LIB}${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
    endforeach (QT_LIB)
endif ()

set_target_properties(MediaTranscoder PROPERTIES
        ${BUNDLE_ID_OPTION}
        MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
        MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
        MACOSX_BUNDLE TRUE
        WIN32_EXECUTABLE TRUE
)
